//
//  PGSettingViewController.m
//  youqu
//
//  Created by 彭鹏 on 16/10/3.
//  Copyright © 2016年 pengpeng. All rights reserved.
//

#define catcheDirectory NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]

#import "PGSettingViewController.h"
#import "PGClearCacheViewCell.h"
#import <SDWebImage/SDImageCache.h>
#import "PGFileManager.h"
#import <SVProgressHUD/SVProgressHUD.h>


@interface PGSettingViewController ()
/**catchSize*/
@property (nonatomic, assign) NSUInteger catchSize;

@end

@implementation PGSettingViewController

static NSString *const cellID = @"settingCellID";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTableView];
    
//    [[[SDImageCache alloc] init] cleanDisk];
//    [[[SDImageCache alloc] init] clearDisk];
    
    NSLog(@"%@",NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]);
    NSUInteger cacheSize = [[SDImageCache sharedImageCache] getSize];
    NSLog(@"cacheSize:%zd",cacheSize);
    
    
    [SVProgressHUD showWithStatus:@"正在计算缓存..."];
    //获取缓存大小
    [PGFileManager getFileSize:catcheDirectory completeBlock:^(NSUInteger fileSize){
        [SVProgressHUD dismiss];
        _catchSize = fileSize;
        [self.tableView reloadData];
    }];
    
    
}


#pragma mark -初始化TableView
- (void)initTableView {
    self.view.backgroundColor = commonBgColor;
    self.tableView.tableFooterView = nil;
    self.tableView.sectionFooterHeight = 0;
}

#pragma mark -TableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

#pragma mark -TableView delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PGClearCacheViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell == nil) {
        cell = [[PGClearCacheViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"清除缓存";
        NSString *sizeString = @"";
        CGFloat size = 0;
        if (_catchSize != 0) {
            if (_catchSize > 1000 * 1000){
                size = _catchSize/1000.0/1000.0;
                sizeString = [NSString stringWithFormat:@"清除缓存(%.1fM)",size];
            }else if (_catchSize > 1000){
                size = _catchSize/1000.0;
                sizeString = [NSString stringWithFormat:@"清除缓存(%.1fKB)",size];
            }else {
                size = _catchSize;
                sizeString = [NSString stringWithFormat:@"清除缓存(%zdB)",size];
            }
            
            cell.textLabel.text = sizeString;
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        
        //清空catche文件夹
        [PGFileManager removeFile:catcheDirectory];
        _catchSize = 0;
        [tableView reloadData];
        
    }
}





@end
